package com.linghang.wusthelper.spider.jwc;

import com.linghang.wusthelper.entity.primary.SelectedCourseInfo;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

/**
 * @author origin
 * 已选课程列表爬虫
 */
public class SelectedCourseSpider implements PageProcessor {

    private String lock;

    private List<SelectedCourseInfo> selectedCourseInfoList;

    private Site site = Site.me()
            .setRetryTimes(3)
            .setTimeOut(10000)
            .addHeader("Connection", "keep-alive")
            .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Encoding", "gzip,deflate,sdch")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36")
            .addHeader("Cache-Control","no-cache")
            .addHeader("Host","jwxt.wust.edu.cn")
            .setDomain("jwxt.wust.edu.cn");

    public SelectedCourseSpider(String JSESSIONID,String SERVERID,String lock,List<SelectedCourseInfo> selectedCourseInfoList){
        site.addCookie("JSESSIONID",JSESSIONID);
        site.addCookie("SERVERID",SERVERID);
        this.lock = lock;
        this.selectedCourseInfoList = selectedCourseInfoList;
    }

    @Override
    public void process(Page page) {
        Selectable selectable = page.getHtml().xpath("//*[@id=\"mxh\"]/tbody");
        List<String> courseNames = selectable.xpath("//tr/td[2]/text()").all();
        List<String> courseUnits = selectable.xpath("//tr/td[3]/text()").all();
        List<String> directions = selectable.xpath("//tr/td[4]/text()").all();
        List<String> credits = selectable.xpath("//tr/td[5]/text()").all();
        List<String> teachers = selectable.xpath("//tr/td[6]/@title").all();
        List<String> classNo = selectable.xpath("//tr/td[7]/text()").all();
        List<String> weeks = selectable.xpath("//tr/td[8]/text()").all();
        List<String> courseTimes = selectable.xpath("//tr/td[9]/text()").all();
        List<String> coursePlaces = selectable.xpath("//tr/td[10]/text()").all();
        List<String> courseStages = selectable.xpath("//tr/td[11]/text()").all();
        List<String> courseCategorys = selectable.xpath("//tr/td[12]/text()").all();
        List<String> courseNature = selectable.xpath("//tr/td[13]/text()").all();
        List<String> groupNmaes = selectable.xpath("//tr/td[14]/text()").all();
        List<String> sexs = selectable.xpath("//tr/td[15]/text()").all();
        List<String> selectedClass = selectable.xpath("//tr/td[16]/text()").all();
        List<String> bookNames = selectable.xpath("//tr/td[17]/text()").all();
        List<String> urls = selectable.xpath("//tr/td[18]/a/@onclick").regex("vJsMod\\('(.*?)',").all();
        int size = courseNames.size();
        for(int i = 0;i < size;i++){
            SelectedCourseInfo selectedCourse = new SelectedCourseInfo();
            selectedCourse.setCourseName(courseNames.get(i).replaceAll(" ",""));
            selectedCourse.setCourseUnit(courseUnits.get(i).replaceAll(" ",""));
            selectedCourse.setDirection(directions.get(i).replaceAll("",""));
            selectedCourse.setCourseCredit(credits.get(i).replaceAll(" ",""));
            selectedCourse.setTeacher(teachers.get(i).replaceAll(" ",""));
            selectedCourse.setClassNo(classNo.get(i).replaceAll(" ",""));
            selectedCourse.setWeeks(weeks.get(i).replaceAll(" ",""));
            selectedCourse.setCourseTime(courseTimes.get(i).replaceAll(" ",""));
            selectedCourse.setCoursePlace(coursePlaces.get(i).replaceAll(" ",""));
            selectedCourse.setCourseStage(courseStages.get(i).replaceAll(" ",""));
            selectedCourse.setCourseCategory(courseCategorys.get(i).replaceAll(" ",""));
            selectedCourse.setCourseNature(courseNature.get(i).replaceAll(" ",""));
            selectedCourse.setGroupName(groupNmaes.get(i).replaceAll(" ",""));
            selectedCourse.setSex(sexs.get(i).replaceAll(" ",""));
            selectedCourse.setSelectedClass(selectedClass.get(i).replaceAll(" ",""));
            selectedCourse.setBookName(bookNames.get(i).replaceAll(" ",""));
            selectedCourse.setUrl(urls.get(i));
            selectedCourseInfoList.add(selectedCourse);
        }
        synchronized (lock){
            lock.notify();
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
